package tmproject.hlhj.fhp.anwser.activitys

import android.content.Intent
import android.graphics.Color
import android.view.View
import android.widget.Toast
import com.bumptech.glide.Glide
import com.lzy.okgo.OkGo
import com.lzy.okgo.cookie.CookieJarImpl
import com.lzy.okgo.cookie.store.SPCookieStore
import com.lzy.okgo.interceptor.HttpLoggingInterceptor
import kotlinx.android.synthetic.main.aty_anwserhome.*
import okhttp3.OkHttpClient
import okhttp3.Response
import tmproject.hlhj.fhp.anwser.BaseAty
import tmproject.hlhj.fhp.anwser.Contents
import tmproject.hlhj.fhp.anwser.R
import tmproject.hlhj.fhp.anwser.bean.*
import tmproject.hlhj.fhp.anwser.customview.AnwserDilaog
import tmproject.hlhj.fhp.anwser.customview.LoginDialog
import tmproject.hlhj.fhp.anwser.http.*
import tmproject.hlhj.fhp.anwser.utils.LoadingUtils
import tmproject.hlhj.fhp.anwser.utils.MyUtils
import tmproject.hlhj.fhp.anwser.utils.SPHelper
import java.util.logging.Level

/**
 * Created by Never Fear   on 2018\8\20 0020.
Never More....
 */
class AnwserHomeAty:BaseAty() {
    private var nAid=0
    var loginDialog:LoginDialog?=null
    private var rid=0
    private var ntype=1
    override fun setListener() {
        btStart.setOnClickListener {
            Apis.getActiveDetail(nAid,SPHelper.getToken(this@AnwserHomeAty)!!)
                    ?.let {
                        it.doOnSubscribe(LoadingAction(this))
                                .subscribe(object :BaseAction<com.lzy.okgo.model.Response<ActiveDetailBean>>{
                                    override fun httpSuccess(t: com.lzy.okgo.model.Response<ActiveDetailBean>) {
                                        if (t.body().data.has_answered==1){
                                            Toast.makeText(this@AnwserHomeAty,"您已经参与过此活动",Toast.LENGTH_SHORT).show()
                                        }else{
                                            if (Contents.Token.isEmpty()){
                                                loginDialog?.show()
                                            }else{
                                                AnwserDilaog(this@AnwserHomeAty,nAid).show()
                                            }
                                        }
                                    }
                                    override fun httpErroCode(baseBean: BaseBean) {
                                        if (baseBean.code==500){
                                            loginDialog?.show()
                                        }
                                    }
                                    override fun httpFail(e: Throwable?) {
                                        LoadingUtils.loadingDismiss()
                                        loginDialog?.show()
                                    }
                                })
                    }
        }
        btNow.setOnClickListener {
            getActive(1)
            ntype=1
            btStart.setBackgroundResource(R.drawable.start_border)
            btStart.isClickable=true
            btNow.setTextColor(Color.WHITE)
            btNext.setTextColor(Color.parseColor("#333333"))
            btNow.setBackgroundResource(R.drawable.select_border)
            btNext.setBackgroundResource(R.drawable.unselect_border)
        }
        btNext.setOnClickListener {
            getActive(2)
            ntype=2
            btStart.isClickable=false
            btStart.setBackgroundResource(R.drawable.start_border_disable)
            btNext.setTextColor(Color.WHITE)
            btNow.setTextColor(Color.parseColor("#333333"))
            btNext.setBackgroundResource(R.drawable.select_border)
            btNow.setBackgroundResource(R.drawable.unselect_border)
        }

        btActive.setOnClickListener {
            hideRule()
        }
        btRule.setOnClickListener {
            getRule(rid)
        }
        btDraw.setOnClickListener {
            val intent = Intent()

            startActivity(intent)
        }
    }

    override fun initView() {
        Contents.Token=SPHelper.getToken(this)!!

        OkGo.getInstance().init(application)
        val builder = OkHttpClient.Builder()
        builder.cookieJar(CookieJarImpl(SPCookieStore(this@AnwserHomeAty)))

        loginDialog=LoginDialog(this)
        loginDialog?.listener=object :LoginDialog.LoginListener{
            override fun loginSuccess() {
                despatchData()
            }
        }
        /*Log*/
        val loggingInterceptor = HttpLoggingInterceptor("okgo")
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY)
        loggingInterceptor.setColorLevel(Level.INFO)
        builder.addInterceptor(loggingInterceptor)
        OkGo.getInstance().okHttpClient = builder.build()

    }
    override fun despatchData() {
        getPrize()

        getActive(1)
        /*用户信息*/
        Apis.getUserInfo(SPHelper.getToken(this@AnwserHomeAty)!!)
                ?.let {
                    it.doOnSubscribe(LoadingAction(this@AnwserHomeAty))
                            .subscribe(object :BaseAction<com.lzy.okgo.model.Response<UserInfoBean>>{
                                override fun httpSuccess(t: com.lzy.okgo.model.Response<UserInfoBean>) {
                                    val userInfoBean = t.body()
                                    myPoint.text=userInfoBean.data.points.toString()
                                    myTimes.text=userInfoBean.data.drawable_times.toString()
                                }

                                override fun httpErroCode(t:BaseBean) {
                                    myPoint.text="登录查看"
                                    myTimes.text="登录查看"
                                    myTimes.setOnClickListener {
                                        loginDialog?.show()
                                    }
                                    myPoint.setOnClickListener {
                                        loginDialog?.show()

                                    }
                                }
                                override fun onCompleted() {
                                    LoadingUtils.loadingDismiss()
                                    super.onCompleted()
                                }
                                override fun httpFail(e: Throwable?) {
                                    myPoint.text="登录查看"
                                    myTimes.text="登录查看"
                                    myTimes.setOnClickListener {
                                        loginDialog?.show()
                                    }
                                    myPoint.setOnClickListener {
                                        loginDialog?.show()

                                    }
                                }
                            })
                }
    }
    //1-当前活动 2-下棋活动
    private fun getActive(type:Int) {
        Apis.getActiveList(type)
                ?.let {
                    it.doOnSubscribe(LoadingAction(this))
                            .subscribe(object : BaseAction<com.lzy.okgo.model.Response<ActiveBean>> {
                                override fun httpSuccess(t: com.lzy.okgo.model.Response<ActiveBean>) {
                                    endTime.visibility=View.VISIBLE

                                    lo_x.visibility=View.VISIBLE
                                    dv.visibility=View.VISIBLE
                                    endTime.text= "${MyUtils.timeStampToStrYMD(t.body().data.end_time.toLong())}截止"
                                    prize.text  = "${t.body().data.title}"
                                    if (type==1){
                                        nAid=t.body().data.id
                                    }
                                    rid=t.body().data.id
                                }
                                override fun httpErroCode(t: BaseBean) {
                                    endTime.visibility=View.VISIBLE
                                    lo_x.visibility=View.GONE
                                    dv.visibility=View.GONE
                                    if (type==1){
                                        endTime.text="活动已结束"
                                        btStart.isClickable=false
                                        btStart.setBackgroundResource(R.drawable.start_border_disable)
                                    }else{
                                        endTime.text="暂无计划"
                                    }
                                }
                                override fun httpFail(e: Throwable?) {
                                    LoadingUtils.loadingDismiss()
                                    endTime.visibility=View.VISIBLE
                                    lo_x.visibility=View.GONE
                                    dv.visibility=View.GONE
                                    if (type==1){
                                        endTime.text="活动已结束"
                                        btStart.isClickable=false
                                        btStart.setBackgroundResource(R.drawable.start_border_disable)
                                    }else{
                                        endTime.text="暂无计划"
                                    }
                                }
                            })
                }
    }
    /*规则*/
    private fun getRule(type:Int){
        Apis.getActiveList(ntype)
                ?.let {
                    it.doOnSubscribe(LoadingAction(this))
                            .subscribe(object : BaseAction<com.lzy.okgo.model.Response<ActiveBean>>{
                                override fun httpSuccess(t: com.lzy.okgo.model.Response<ActiveBean>) {
                                    showRule()
                                    tvObject.text=t.body().data.`object`
                                    tvDetail.text=t.body().data.rule
                                }

                                override fun httpErroCode(baseBean: BaseBean) {
                                }

                                override fun httpFail(e: Throwable?) {
                                }
                            })
                }
        /*Apis.getActiveDetail(id,SPHelper.getToken(this@AnwserHomeAty)!!)
                ?.let {
                    it.doOnSubscribe(LoadingAction(this))
                            .subscribe(object :BaseAction<com.lzy.okgo.model.Response<ActiveDetailBean>>{
                                override fun httpSuccess(t: com.lzy.okgo.model.Response<ActiveDetailBean>) {
                                    showRule()
                                    tvObject.text=t.body().data.`object`
                                    tvDetail.text=t.body().data.rule
                                }

                                override fun httpErroCode(t: BaseBean) {
                                }

                                override fun httpFail(e: Throwable?) {
                                    LoadingUtils.loadingDismiss()
                                    loginDialog?.show()
                                }
                            })
                }*/
    }

    private fun getPrize(){
        Apis.getPrize()
                ?.let {
                    it
                            .doOnSubscribe{
                            }
                            .subscribe(object :BaseAction<com.lzy.okgo.model.Response<PrizeBean>>{
                                override fun httpSuccess(t: com.lzy.okgo.model.Response<PrizeBean>) {
                                    val prizeBean = t.body()
                                    Glide.with(this@AnwserHomeAty).load(Urls.BaseUrl+prizeBean.data[0].pic)
                                            .apply(GlideTools.LoadOption())
                                            .into(prizeIcon)
                                }

                                override fun httpErroCode(baseBean: BaseBean) {
                                    LoadingUtils.loadingDismiss()
                                }

                                override fun httpFail(e: Throwable?) {
                                    LoadingUtils.loadingDismiss()
                                }
                            })
                }
    }
    fun refresh(){
        despatchData()
    }
    private fun showRule(){
        ruleObject.visibility=View.VISIBLE
        ruleDetail.visibility=View.VISIBLE
        lo_1.visibility=View.VISIBLE
        lo_2.visibility=View.VISIBLE
        btStart.visibility=View.GONE
        loInfo.visibility=View.GONE
        btActive.visibility=View.VISIBLE
        btRule.visibility=View.GONE
    }
    private fun hideRule(){
        ruleObject.visibility=View.GONE
        ruleDetail.visibility=View.GONE
        lo_1.visibility=View.GONE
        lo_2.visibility=View.GONE
        btStart.visibility=View.VISIBLE
        loInfo.visibility=View.VISIBLE
        btActive.visibility=View.GONE
        btRule.visibility=View.VISIBLE
    }
    override fun getContentId(): Int {
        return R.layout.aty_anwserhome
    }
}